package com.common.util.Excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.DateUtils;
import com.google.common.base.Charsets;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

public class ExcelUtil {
    public ExcelUtil() {
    }

    public static <T> List<T> read(MultipartFile excel, Class<T> clazz) {
        DataListener dataListener = new DataListener();
        ExcelReaderBuilder builder = getReaderBuilder(excel, (ReadListener<T>) dataListener, clazz);
        if (builder == null) {
            return null;
        } else {
            builder.doReadAll();
            return (List<T>) dataListener.getDataList();
        }
    }

    public static <T> List<T> read(MultipartFile excel, int sheetNo, Class<T> clazz) {
        return read(excel, sheetNo, 1, clazz);
    }

    public static <T> List<T> read(MultipartFile excel, int sheetNo, int headRowNumber, Class<T> clazz) {
        DataListener dataListener = new DataListener();
        ExcelReaderBuilder builder = getReaderBuilder(excel, (ReadListener<T>)dataListener, clazz);
        if (builder == null) {
            return null;
        } else {
            builder.sheet(sheetNo).headRowNumber(headRowNumber).doRead();
            return (List<T>) dataListener.getDataList();
        }
    }



    public static <T> void save(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) {
        ImportListener importListener = new ImportListener( importer);
        ExcelReaderBuilder builder = getReaderBuilder(excel, (ReadListener<T>)importListener, clazz);
        if (builder != null) {
            builder.doReadAll();
        }

    }

    public static <T> void export(HttpServletResponse response, List<T> dataList, Class<T> clazz) {
        try {
            export(response, DateUtils.format(new Date(), "yyyyMMddHHmmss"), "导出数据", dataList, clazz);
        } catch (Throwable var4) {
            throw var4;
        }
    }

    public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
        } catch (Throwable var6) {
             var6.printStackTrace();
        }
    }
    public static <T> ExcelReaderBuilder getReaderBuilder(MultipartFile excel, ReadListener<T> readListener, Class<T> clazz) {
        String filename = excel.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            throw new ExcelException("请上传文件!");
        } else if (!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx")) {
            throw new ExcelException("请上传正确的excel文件!");
        } else {
            try {
                InputStream inputStream = new BufferedInputStream(excel.getInputStream());
                return EasyExcel.read(inputStream, clazz, readListener);
            } catch (IOException var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

}
